package com.lm.scau.framedemos.Rx;

import android.os.Bundle;
import android.util.Log;

import com.lm.scau.framedemos.BaseActivity;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Action0;
import rx.functions.Action1;

/**
 * Function:
 * auth：liming on 2016/6/28 10:09
 * mail：liming@wxchina.com
 */
public class RxJavaActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getmTitleBar().setTitle("RxJava");
        init();
    }

    private void init() {
        Observable observable = Observable.create(new Observable.OnSubscribe<String>(){

            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onNext("hellow");
                subscriber.onNext("my");
                subscriber.onNext("friend");
                subscriber.onCompleted();
            }
        });
        Subscriber subscriber = new Subscriber() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Object o) {

            }
        };
        //这里支持完整定义的回调
        observable.subscribe(subscriber);
        //这里下面是不完整定义的回调，自己定义回调类型，onNext,onError,onComplete
        Action1<String> onNextAction = new Action1<String>() {
            @Override
            public void call(String s) {
                Log.i("onNextAction","自动创建 Subscriber ，并使用 onNextAction 来定义 onNext()");
            }
        };
        //会自己创建subscriber
        observable.subscribe(onNextAction);
        Action1<Throwable> onErrAction = new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                Log.i("onErrAction",throwable.toString());
            }
        };
        Action0 onCompleteAction = new Action0() {
            @Override
            public void call() {
                Log.i("onCompleteAction","onComplete");
            }
        };
        observable.subscribe(onNextAction,onErrAction,onCompleteAction);
    }
}
